﻿using System;
using System.Collections.Generic;
using System.Linq;
using UcAsp.Net.Model;
namespace UcAsp.Net.NOSQLDAL
{
    public class ArticlesDAL : UcAsp.Net.IDAL.IArticlesDAL
    {
        public List<Model.Articles> GetArtces()
        {
            return STSData.GetInstance.Select<Model.Articles>().OrderByDescending(o => o.Rank).ToList();
        }
        public IEnumerable<Model.ArticelsViews> GetArticelsViews()
        {
            
            var query = from a in GetArtces()
                        join c in new CategoriesDAL().GetCategories()
                            on a.CategorieID equals c.ID
                        select new ArticelsViews
                        {
                            AddDatetime = a.AddDatetime,
                            ArticleContent = a.ArticleContent,
                            CategorieID = a.CategorieID,
                            CategorieName = c.CategorieName,
                            Date1 = a.Date1,
                            Date2 = a.Date2,
                            Date3 = a.Date3,
                            Date4 = a.Date4,
                            Date5 = a.Date5,
                            Date6 = a.Date6,
                            Date7 = a.Date7,
                            Date8 = a.Date8,
                            Description = a.Description,
                            Float1 = a.Float1,
                            Float2 = a.Float2,
                            Float3 = a.Float3,
                            Float4 = a.Float4,
                            Float5 = a.Float5,
                            Hot = a.Hot,
                            IconUrl = a.IconUrl,
                            ID = a.ID,
                            Int1 = a.Int1,
                            Int2 = a.Int2,
                            Int3 = a.Int3,
                            Int4 = a.Int4,
                            Int5 = a.Int5,
                            IsRecommend = a.IsRecommend,
                            IsRelease = a.IsRelease,
                            MetaDescrition = a.MetaDescrition,
                            MetaKeyword = a.MetaKeyword,
                            Note1 = a.Note1,
                            Note2 = a.Note2,
                            Note3 = a.Note3,
                            Note4 = a.Note4,
                            Note5 = a.Note5,
                            Note6 = a.Note6,
                            Note7 = a.Note7,
                            Note8 = a.Note8,
                            Note9 = a.Note9,
                            Rate = a.Rate,
                            ShowPath = a.ShowPath,
                            CagegorieShowPath=c.ShowPath,
                            Tag = a.Tag,
                            Title = a.Title,
                            Rank=a.Rank,
                            UserID = a.UserID,
                            ViewsTemp = a.ViewsTemp,
                            
                        };            
            return query;
        }
        public List<Articles> Select(int? userid, int? categoriesid, bool isrecommend, bool isrelease, DateTime? begin, DateTime? end, string keyword, int pagesize, int pageindex, out int pagecount)
        {
            List<Articles> list = new List<Articles>();
            IQueryable<Articles> query = GetArtces() as IQueryable<Articles>;
            if (userid != null)
            {
                query = query.Where(o => o.UserID == userid);
            }
            if (categoriesid != null)
            {
                query = query.Where(o => o.CategorieID == categoriesid);
            }
            if (isrecommend)
            {
                query = query.Where(o => o.IsRecommend == true);
            }
            if (isrelease)
            {
                query = query.Where(o => o.IsRelease == true);
            }

            if (begin != null)
            {
                query = query.Where(o => o.AddDatetime > begin);
            }
            if (end != null)
            {
                query = query.Where(o => o.AddDatetime < end);
            }
            if (keyword != null)
            {
                query = query.Where(o => o.Tag.Contains(keyword) || o.Title.Contains(keyword) || o.MetaKeyword.Contains(keyword) || o.MetaDescrition.Contains(keyword));
            }
            pagecount = (query.Count() / pagesize) + 1;
            list = query.OrderByDescending(o => o.Rank).ThenByDescending(o => o.ID).Skip(pagesize * pageindex).Take(pagesize).ToList();
            return list;
        }
        public List<ArticelsViews> SelectViews(int? userid, int? categoriesid, bool? isrecommend, bool? isrelease, DateTime? begin, DateTime? end, string keyword, int pagesize, int pageindex, out int pagecount)
        {
            List<ArticelsViews> list = new List<ArticelsViews>();
            IEnumerable<ArticelsViews> query = GetArticelsViews();
            if (query == null)
            {
                pagecount = 0;
                return list;
            }

            if (categoriesid != null)
            {
                query = query.Where(o => o.CategorieID == categoriesid);
            }
            if (userid != null)
            {
                query = query.Where(o => o.UserID == userid);
            }
            if (isrecommend != null)
            {
                query = query.Where(o => o.IsRecommend == isrecommend);
            }
            if (isrelease != null)
            {
                query = query.Where(o => o.IsRelease == isrelease);
            }
            if (begin != null)
            {
                query = query.Where(o => o.AddDatetime > begin);
            }

            if (end != null)
            {
                query = query.Where(o => o.AddDatetime < end);
            }
            if (keyword != null)
            {
                query = query.Where(o => o.Title.Contains(keyword) || o.Tag.Contains(keyword) || o.MetaKeyword.Contains(keyword) || o.MetaDescrition.Contains(keyword));
            }

            pagecount = (query.Count() / pagesize) + 1;
            list = query.OrderByDescending(o => o.Rank).ThenByDescending(o => o.ID).Skip(pagesize * pageindex).Take(pagesize).ToList();
            return list;
        }

        public Model.Articles Insert(Articles model)
        {
            long key;
            DateTime datetime = Convert.ToDateTime("1970-01-01");
            Model.Articles lastrow = STSData.GetInstance.LastRow<Model.Articles>(out key);
            model.ID = Convert.ToInt32(key.ToString()) + 1;
            model.IconUrl = model.IconUrl == null ? "" : model.IconUrl;
            model.Note1 = model.Note1 == null ? "" : model.Note1;
            model.Note2 = model.Note2 == null ? "" : model.Note2;
            model.Note3 = model.Note3 == null ? "" : model.Note3;
            model.Note4 = model.Note4 == null ? "" : model.Note4;
            model.Note5 = model.Note5 == null ? "" : model.Note5;
            model.Note6 = model.Note6 == null ? "" : model.Note6;
            model.Note7 = model.Note7 == null ? "" : model.Note7;
            model.Note8 = model.Note8 == null ? "" : model.Note8;
            model.Note9 = model.Note9 == null ? "" : model.Note9;

            model.Date1 = model.Date1 == DateTime.Parse("0001-01-01 00:00:00") ? datetime : model.Date1;
            model.Date2 = model.Date2 == DateTime.Parse("0001-01-01 00:00:00") ? datetime : model.Date2;
            model.Date3 = model.Date3 == DateTime.Parse("0001-01-01 00:00:00") ? datetime : model.Date3;
            model.Date4 = model.Date4 == DateTime.Parse("0001-01-01 00:00:00") ? datetime : model.Date4;
            model.Date5 = model.Date5 == DateTime.Parse("0001-01-01 00:00:00") ? datetime : model.Date5;
            model.Date6 = model.Date6 == DateTime.Parse("0001-01-01 00:00:00") ? datetime : model.Date6;
            model.Date7 = model.Date7 == DateTime.Parse("0001-01-01 00:00:00") ? datetime : model.Date7;
            model.Date8 = model.Date8 == DateTime.Parse("0001-01-01 00:00:00") ? datetime : model.Date8;

            STSData.GetInstance.Insert<Model.Articles>(model.ID, model);
            return model;
        }
        public bool Update(Articles model)
        {
            STSData.GetInstance.Update<Model.Articles>(model.ID, model);
            return true;
        }
        public bool Delete(Articles model)
        {
            STSData.GetInstance.Delete<Model.Articles>(model.ID);
            return true;
        }
        public bool Delete(DateTime Time, int CategoriesID)
        {
            List<Articles> list = GetArtces().Where(o => o.AddDatetime < Time).ToList();

            foreach (Articles model in list)
            {
                Delete(model);
            }
            return true;
        }
        public Articles Limt(int id)
        {
            return GetArtces().FirstOrDefault(o => o.ID == id);
        }
        public Articles GetArtieByTitle(string title)
        {
            return GetArtces().FirstOrDefault(o => o.Title == title);
        }
        public ArticelsViews GetArtieByShowPath(string showpath, out int count, out List<ArticelsViews> nextorprev)
        {
            Model.Articles model = GetArtces().FirstOrDefault(o => o.ShowPath == showpath);
            Model.ArticelsViews result=new  ArticelsViews();
            List<ArticelsViews> list = new List<ArticelsViews>();
            nextorprev = list;
            count = 0;
            IEnumerable<ArticelsViews> Views = GetArticelsViews();
            if (model != null)
            {

                model.Date1 = DateTime.Now;
                model.Hot++;
                Update(model);
                result = Views.FirstOrDefault(o => o.ShowPath == showpath);

                Model.ArticelsViews nextmodel = Views.OrderByDescending(o => o.ID).Where(o => o.CategorieID == model.CategorieID).FirstOrDefault(o => o.ID < model.ID);
                list.Add(nextmodel);
                Model.ArticelsViews prevmodel = Views.OrderBy(o => o.ID).Where(o => o.CategorieID == model.CategorieID).FirstOrDefault(o => o.ID > model.ID);
                list.Add(prevmodel);
                nextorprev = list;
                count = new ArticlesPageDAL().GetArticlesPage().Where(o => o.ArticlesID == model.ID).Count();
                return result;
            }
            else
            {
                return null;
            }
        }
        public ArticelsViews GetArtieById(int id, out int count, out List<ArticelsViews> nextorprev)
        {
            Model.Articles model = GetArtces().FirstOrDefault(o => o.ID == id);
            if (model != null)
            {
                model.Date1 = DateTime.Now;
                model.Hot++;
                Update(model);
            }
            Model.ArticelsViews result = GetArticelsViews().FirstOrDefault(o => o.ID == id);
            count = new ArticlesPageDAL().GetArticlesPageByArticlesID(id).Count;
            IEnumerable<ArticelsViews> Views = GetArticelsViews();
            List<ArticelsViews> list = new List<ArticelsViews>();
            Model.ArticelsViews nextmodel = Views.OrderByDescending(o => o.ID).Where(o => o.CategorieID == model.CategorieID).FirstOrDefault(o => o.ID < model.ID);
            list.Add(nextmodel);
            Model.ArticelsViews prevmodel = Views.OrderBy(o => o.ID).Where(o => o.CategorieID == model.CategorieID).FirstOrDefault(o => o.ID > model.ID);
            list.Add(prevmodel);
            nextorprev = list;
            return result;
        }

        public Articles RecommdRecord(int id, bool recommd)
        {
            Model.Articles model = Limt(id);
            model.IsRecommend = recommd;
            Update(model);
            return model;
        }
        public Articles ReleaseRecord(int id, bool release)
        {
            Model.Articles model = Limt(id);
            model.IsRelease = release;
            Update(model);
            return model;
        }
        public List<ArticelsViews> Subject(string categoriesid, string keyword, int maxnum, int whoview)
        {
            string[] result = categoriesid.Split(',');
            int[] catid = new int[result.Length];
            for (int i = 0; i < result.Length; i++)
            {
                catid[i] = int.Parse(result[i]);
            }
            List<ArticelsViews> list = new List<ArticelsViews>();
            var query = GetArticelsViews();
            if (query == null)
                return list;
            query = from p in query
                    where (catid).Contains(p.CategorieID) && (p.Title.Contains(keyword) || p.Tag.Contains(keyword)) && p.IsRelease == true
                    select p;
            switch (whoview)
            {
                case 1:
                    query = query.Where(o => !string.IsNullOrEmpty(o.IconUrl)).OrderByDescending(o => o.Date1);
                    break;

                case 2:
                    query = query.Where(o => !string.IsNullOrEmpty(o.IconUrl)).OrderByDescending(o => o.AddDatetime);
                    break;

                case 3:
                    query = query.Where(o => o.IsRecommend == true && !string.IsNullOrEmpty(o.IconUrl)).OrderByDescending(o => o.AddDatetime);
                    break;

                case 4:
                    query = query.Where(o => !string.IsNullOrEmpty(o.IconUrl)).OrderByDescending(o => o.Hot);
                    break;

                case 5:
                    query = query.Where(o => !string.IsNullOrEmpty(o.IconUrl)).OrderByDescending(o => o.AddDatetime);
                    break;

                case 6:
                    query = query.Where(o => !string.IsNullOrEmpty(o.IconUrl) && o.IsRecommend == true).OrderByDescending(o => o.AddDatetime);
                    break;

                default:
                    break;
            }
            query = query.Take(maxnum);
            list = query.ToList();
            return list;
        }
    }
}